<?php


namespace app\api\controller\hd;

use app\common\controller\Api;
use think\Db;
use \app\admin\model\hd\activity\Info as InfoModel;
use \app\admin\model\hd\activity\Type as TypeModel;
use \app\admin\model\hd\activity\Log as LogModel;



class Activity extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 获取活动类别
     */
    public function type()
    {
        $this->success('获取成功', TypeModel::select());
    }

    /**
     * 获取志愿活动列表
     */
    public function volunteerList($type_id = 0)
    {
        $where = [];
        if ($type_id != 0) {
            $where["type_id"] = $type_id;
        }
        $list = InfoModel::with("type")
            ->where($where)
            ->where("forms", 1)
            ->where("status", "normal")
            ->order("create_time", "desc")
            ->paginate();

        $this->success('获取成功', $list);
    }

    /**
     * 获取投票活动列表
     */
    public function voteList($type_id = 0)
    {
        $where = [];
        if ($type_id != 0) {
            $where["type_id"] = $type_id;
        }
        $list = InfoModel::with("type")
            ->where($where)
            ->where("forms", 2)
            ->where("status", "normal")
            ->order("create_time", "desc")
            ->paginate();

        $this->success('获取成功', $list);
    }

    /**
     * 活动详情
     */
    public function details($id)
    {
        $details = InfoModel::with("type")->where(InfoModel::getTable() . ".id", $id)->find();
        $details['user_log'] = LogModel::where("act_id", $id)->where("user_id", $this->auth->id)->find();
        if ($details["forms"] == 1) {
            if ($details->reg_start_time > time()) {
                $details->state = 0; //待报名
            }
            if ($details->reg_start_time < time() && $details->reg_start_time > time()) {
                $details->state = 1; //可报名
            }
            if ($details->reg_start_time < time()) {
                $details->state = 2; //报名结束
            }
            if ($details->start_time - 86400 < time() && $details->start_time - 1800 > time()) {
                $details->state = 3; //待签到
            }
            if ($details->start_time - 1800 < time() && $details->start_time + 1800 > time()) {
                $details->state = 4; //可签到（前后半小时）
            }
            if ($details->start_time + 1800 < time()) {
                $details->state = 5; //签到结束
            }
            $details["registration_num"] = LogModel::where("act_id", $id)->where("registration_results", 1)->count(); //报名数量
            $details["sign_num"] = LogModel::where("act_id", $id)->where("sign", 1)->count(); //签到数量
        }
        if ($details["forms"] == 2) {
            if ($details->reg_start_time > time()) {
                $details->state = 0; //待投票
            }
            if ($details->reg_start_time < time() && $details->reg_start_time > time()) {
                $details->state = 1; //可投票
            }
            if ($details->reg_start_time < time()) {
                $details->state = 2; //投票结束
            }
            $details["vote_num"] = LogModel::where("act_id", $id)->count();
            $details["vote_abandon_num"] = LogModel::where("act_id", $id)->where("vote_results", 0)->count(); //弃权票
            $details["vote_agree_num"] = LogModel::where("act_id", $id)->where("vote_results", 1)->count(); //同意
            $details["vote_oppose_num"] = LogModel::where("act_id", $id)->where("vote_results", 2)->count(); //反对
        }

        $this->success('获取成功', $details);
    }

    /**
     * 活动报名
     */
    public function enroll($id)
    {
        $enroll = LogModel::where("act_id", $id)->where("user_id", $this->auth->id)->find();
        if ($enroll) {
            if ($enroll->registration_results == 2) {
                $update = LogModel::where("act_id", $id)->where("user_id", $this->auth->id)->update(["registration_results" => 1]);
                if ($update) {
                    $this->success('报名成功');
                }
            }
            if ($enroll->registration_results == 1) {
                $update = LogModel::where("act_id", $id)->where("user_id", $this->auth->id)->update(["registration_results" => 2]);
                if ($update) {
                    $this->success('已取消');
                }
            }
        } else {
            $insert = LogModel::insert(["act_id" => $id, "user_id" => $this->auth->id, "registration_results" => 1, "create_time" => time()]);
            if ($insert) {
                $this->success('报名成功');
            }
        }
        $this->error('报名失败');
    }

    /**
     * 我的活动报名
     */
    public function user_volunteerList()
    {
        $list = InfoModel::with(['log' => function ($query) {
            $query->where('user_id', $this->auth->id);
        }])
            ->where("forms", 1)
            ->where("status", "normal")
            ->order("create_time", "desc")
            ->paginate()
            ->each(function ($info, $key) {
                $info->type = TypeModel::where("id", $info->type_id)->find();
            });
        $this->success('获取成功', $list);
    }

    /**
     * 活动签到
     */
    public function sign($id)
    {
        $update = LogModel::where("act_id", $id)->where("user_id", $this->auth->id)->update(["sign" => 1]);
        if ($update) {
            $this->success('签到成功');
        }
        $this->error('签到失败');
    }

    /**
     * 投票
     */
    public function vote()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post();
            $params['user_id'] = $this->auth->id;
            $params['create_time'] = time();
            $insert = LogModel::insert($params);
            if ($insert) {
                $this->success('投票成功');
            }
            $this->error('投票失败');
        }
        $this->error('非法请求');
    }

    /**
     * 我的活动投票
     */
    public function user_voteList()
    {
        $list = InfoModel::with(['log' => function ($query) {
            $query->where('user_id', $this->auth->id);
        }])
            ->where("forms", 2)
            ->where("status", "normal")
            ->order("create_time", "desc")
            ->paginate()
            ->each(function ($info, $key) {
                $info->type = TypeModel::where("id", $info->type_id)->find();
            });
        $this->success('获取成功', $list);
    }
}
